<template>
    <addEditClassinfoView ref="addEditClassinfoView" />
    <addEditClassinfoBatch ref="addEditClassinfoBatch" />
    <addFileView ref="addFileView" />
    <div class="app-table">
        <div class="app-body">
            <div class="app-search">
                <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="所在年级" :label-width="formLabelWidth" prop="gradeId">
                        <el-select v-model="form.gradeId" autocomplete="off" placeholder="请选择年级" clearable
                            style="width: 192px;" @change="changeGradeId">
                            <el-option v-for="gradeId in gradeIds" :key="gradeId" :value="gradeId"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="班级名称">
                        <el-input placeholder="请输入班级名称" v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="serach_btn" type="primary" plain @click="onSubmit"
                            @keyup.enter="searchEnter">搜索</el-button>
                        <el-button class="add_btn" type="success" @click="add" plain>添加</el-button>
                        <el-button class="export_btn" type="warning" @click="addBatch" plain>批量创建班级</el-button>
                        <!-- <el-button class="update_btn" type="warning" @click="updateHandle" plain>一键更新权限</el-button> -->
                    </el-form-item>
                </el-form>
            </div>
            <el-table size="default" :data="list" style="width: 100%" :row-class-name="tableRowClassName">
                <el-table-column prop="id" label="id" width="90" />
                <el-table-column prop="name" label="班级名称" />
                <el-table-column prop="teacherName" label="班主任" />
                <el-table-column prop="teacherNumber" label="人员工号" />
                <!-- <el-table-column prop="alias" label="别名"/> -->
                <!-- <el-table-column prop="level" label="级别"/> -->
                <!-- <el-table-column prop="type" label="类型"/> -->
                <!-- <el-table-column prop="status" label="状态"/> -->
                <!-- <el-table-column prop="campus" label="校区、默认主校区"/> -->
                <!-- <el-table-column prop="classType" label="班级类型"/> -->
                <!-- <el-table-column prop="subordinateDepartment" label="所属级部"/> -->
                <el-table-column prop="subordinateDepartmentName" label="所属级部" />
                <!-- <el-table-column prop="gradeId" label="年级"/> -->
                <!-- <el-table-column prop="classId" label="编号"/> -->
                <!-- <el-table-column prop="subjectType" label="学科类型"/> -->
                <!-- <el-table-column prop="isDelete" label=""/> -->
                <!-- <el-table-column prop="classMotto" label=""/> -->
                <!-- <el-table-column prop="schoolYear" label=""/> -->
                <!-- <el-table-column prop="thirdClassCode" label=""/> -->
                <el-table-column label="操作" width="140px">
                    <template style="display: block" #default="scope">
                        <el-button class="edit_btn" size="small" type="success" @click="editClassinfo(scope.row)">编辑
                        </el-button>
                        <el-button class="del_btn" size="small" type="danger" @click="deleteClassinfo(scope.row.id)">删除
                        </el-button>
                    </template>
                </el-table-column>
                <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <el-config-provider :locale="locale">
                <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                    layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                    @current-change="changeCurrentPage">
                </el-pagination>
            </el-config-provider>
        </div>
        <bottomView />
    </div>
</template>
<script>
import addEditClassinfoView from "@/components/baseInfo/addEditClassinfo.vue";
import addEditClassinfoBatch from "@/components/baseInfo/addEditClassinfoBatch.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
    name: "ClassinfoListView",
    data() {
        return {
            type: 0,
            title: "",
            page: 0,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            gradeIds: [],
            form: {
                name: "",
                alias: "",
                subordinateDepartmentName: "",
                classMotto: "",
                schoolYear: "",
                thirdClassCode: "",
                teacherName: "",
                gradeId: "",
                teacherNumber: "",
            },
        };
    },
    components: {
        addEditClassinfoView, addEditClassinfoBatch,
        addFileView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        ElMessage, Upload, bottomView
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getClassinfoList();
        this.getGradeIds();
    },
    watch: {
        $route() {
            this.getClassinfoList();
        },
    },
    methods: {
        getGradeIds() {
            let date = new Date();
            let nowYear = parseInt(date.getFullYear());
            let nowMonth = parseInt(date.getMonth() + 1);
            let gradeIds = [];
            if (nowMonth > 6) {
                gradeIds.push(nowYear - 2);
                gradeIds.push(nowYear - 1);
                gradeIds.push(nowYear);
            } else {
                gradeIds.push(nowYear - 3);
                gradeIds.push(nowYear - 2);
                gradeIds.push(nowYear - 1);
            }
            console.log(123, gradeIds);
            this.gradeIds = gradeIds;
        },
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getClassinfoList();
            }
        },
        changeGradeId() {
            this.page = 1
            this.getClassinfoList()
        },
        add() {
            this.$refs.addEditClassinfoView.title = "添加班级"
            this.$refs.addEditClassinfoView.getGradeIds();
            this.$refs.addEditClassinfoView.getBaseTeacherListApp();
            this.$refs.addEditClassinfoView.personnelType = this.type
            this.$refs.addEditClassinfoView.dialogFormVisible = true;
        },
        addBatch() {
            this.$refs.addEditClassinfoBatch.title = "批量创建班级"
            this.$refs.addEditClassinfoBatch.getGradeIds();
            this.$refs.addEditClassinfoBatch.getBaseDepartmentList();
            this.$refs.addEditClassinfoBatch.dialogFormVisible = true;
        },
        editClassinfo(obj) {
            this.$refs.addEditClassinfoView.title = "设置班主任"
            this.$refs.addEditClassinfoView.getClassinfoById(obj.id);
            this.$refs.addEditClassinfoView.getBaseTeacherListApp();
            this.$refs.addEditClassinfoView.dialogFormVisible = true;
        },
        deleteClassinfo(id) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/baseClassInfo/deleteById", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getClassinfoList();
                            } else {
                                ElMessage.error("删除失败，请重试！");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getClassinfoList()
        },
        importExcel() {
            this.$refs.addFileView.dialogFormVisible = true;
            this.$refs.addFileView.title = "批量导入";
            this.$refs.addFileView.uploadApiUrl = "/javaApi/baseClassInfo/importExcel";
        },
        exportExcel() {
            httpService(
                "/javaApi/baseClassInfo/exportExcel",
                {
                    "name": this.form.name,
                    "alias": this.form.alias,
                    "subordinateDepartmentName": this.form.subordinateDepartmentName,
                    "classMotto": this.form.classMotto,
                    "gradeId": this.form.gradeId,
                    "schoolYear": this.form.schoolYear,
                    "thirdClassCode": this.form.thirdClassCode,
                    "teacherName": this.form.teacherName,
                    "teacherNumber": this.form.teacherNumber,
                },
                "get"
            ).then((data) => {
                if (data.code == 200) {
                    window.open(data.msg, '_blank')
                }
            });
        },
        onSubmit() {
            this.getClassinfoList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getClassinfoList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getClassinfoList();
        },
        tableRowClassName({ row, rowIndex }) {
            return "";
        },
        getClassinfoList() {
            httpService(
                "/javaApi/baseClassInfo/list/" + this.page + "/" + this.pageSize,
                {
                    "name": this.form.name,
                    "alias": this.form.alias,
                    "gradeId": this.form.gradeId ? this.form.gradeId : this.form.gradeId = "",
                    "subordinateDepartmentName": this.form.subordinateDepartmentName,
                    "classMotto": this.form.classMotto,
                    "schoolYear": this.form.schoolYear,
                    "thirdClassCode": this.form.thirdClassCode,
                    "teacherName": this.form.teacherName,
                    "teacherNumber": this.form.teacherNumber,
                },
                "get"
            ).then((data) => {
                this.list = data.list;
                this.total = data.total;
                this.pageCount = data.pages;
            });
        },
        // updateHandle() {
        //     ElMessageBox.confirm("确定要更新权限吗？", "提醒", {
        //         confirmButtonText: "确定",
        //         cancelButtonText: "取消",
        //         type: "warning",
        //     })
        //         .then(() => {
        //             let params = new FormData()
        //             httpService("/javaApi/baseClassInfo/updateMasterRole", params, "post").then(
        //                 (data) => {
        //                     if (data.code == 200) {
        //                         ElMessage.success("更新成功");
        //                         this.getClassinfoList();
        //                     } else {
        //                         ElMessage.error("更新失败，请重试！");
        //                     }
        //                 }
        //             );
        //         })
        //         .catch(() => {
        //         });
        // },
    },
};
</script>
<style scoped>
.el-main {
    padding: 0px;
}

.teacher-content {
    width: 100%;
}

.app-table {
    background: #eff3f5;
    padding: 5px 10px 10px;
    /* width: 100%; */
}

.app-search {}

.app-separator {
    padding-top: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 204px);
}

.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
    text-align: center;
    margin-top: 10px;
}

.serach_btn {
    background-color: #007EFF;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.add_btn {
    background-color: #00c78c;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.export_btn {
    background-color: #FF7200;
    border-radius: 8px;
    color: #fff;
    border: none;
}

.update_btn {
    background-color: #979797;
    border-radius: 8px;
    color: #fff;
    border: none;
}

.edit_btn {
    background-color: #fff;
    color: #007EFF;
    border: none;
}

.del_btn {
    background-color: #fff;
    color: #FF0000;
    border: none;
}
</style>
